﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Collections;
using ITSupportPlatform.DAL;

namespace ITSupportPlatform
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ShartPointService service = new ShartPointService();
            service.Credentials = new System.Net.NetworkCredential("zyinfo", "Founder@2012", "Founder");
            service.Url = "http://zyinfo.founder.com/dept/IT/_vti_bin/Lists.asmx";

            //XmlNode node = service.GetList("");
            XmlNode node = service.GetListItems("ERP知识库", String.Empty, null, ViewFields, "0", QueryOptions, String.Empty);
            //XmlNamespaceManager xnm = new XmlNamespaceManager(
            //xnm.AddNamespace("SOAP-ENV", "http://schemas.xmlsoap.org/soap/envelope/");
            //xnm.AddNamespace("Mo", "http://www.monternet.com/dsmp/schemas/");

            //ReadSharePointData(node);ERP实时公告栏
            XmlDocument doc = new XmlDocument();
            XmlNamespaceManager nsp = new XmlNamespaceManager(doc.NameTable);
            nsp.AddNamespace("rs", "urn:schemas-microsoft-com:rowset");
            nsp.AddNamespace("z", "#RowsetSchema");
            XmlNodeList nodeList = node.SelectNodes("rs:data/z:row", nsp);

            //FounderOAResourceGroupEntities context = new FounderOAResourceGroupEntities();

            foreach (XmlNode item in nodeList)
            {
                string id = item.Attributes["ows_ID"].Value;
                string title = item.Attributes["ows_BaseName"].Value;
                string createTime = item.Attributes["ows_Created"].Value;
                string url = string.Format("http://zyinfo.founder.com/dept/IT/_layouts/DocIdRedir.aspx?ID={0}", item.Attributes["_dlc_DocId"].Value);
                //string users = item.Attributes["ows_relevance"].Value;
                //
                //context.ITSupport_Notices.AddObject(new ITSupport_Notices()
                //{
                //    Id = new Guid(id),
                //    Title = title,
                //    NoticeContent = body,
                //    CreateTime = DateTime.Parse(createTime),
                //    SendUsers = users,
                     

                //});
            }
        }

        public void ReadSharePointData(XmlNode node)
        {
            IEnumerator ienum = node.GetEnumerator();
            while (ienum.MoveNext())
            {
                XmlNode current = (XmlNode)ienum.Current;
                if (current.Name == "z:row")
                {
                    string title = current.Attributes["ows_Title"].Value;
                }
                else
                {
                    foreach (XmlNode item in node.ChildNodes)
                    {
                        ReadSharePointData(item);
                    }
                }
            }
        }

        static XmlNode QueryOptions
        {

            get
            {

                XmlDocument _camlDoc = new XmlDocument();

                XmlElement queryOptions = _camlDoc.CreateElement("QueryOptions");

                queryOptions.InnerXml = "<ExpandUserField>True</ExpandUserField>";

                return queryOptions;

            }

        }

        static XmlNode ViewFields
        {

            get
            {
                XmlDocument _camlDoc = new XmlDocument();
                XmlElement viewFields = _camlDoc.CreateElement("ViewFields");

                System.Text.StringBuilder viewFieldsSB = new System.Text.StringBuilder();

                #region View Fields

                viewFieldsSB.Append(" <FieldRef Name=\"ID\" />");

                viewFieldsSB.Append(" <FieldRef Name=\"BaseName\" />");

                viewFieldsSB.Append(" <FieldRef Name=\"_dlc_DocId\" />");

                viewFieldsSB.Append(" <FieldRef Name=\"Created\" />");

                #endregion

                string sResult = viewFieldsSB.ToString();

                viewFields.InnerXml = viewFieldsSB.ToString();

                return viewFields;

            }
        }
    }
}